package com.Action;

import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import com.Entity.DataInfo;
import com.Entity.Page.Json;
import com.Service.DataInfoService;
import com.alibaba.fastjson.JSONObject;

public class DisplayDataAction extends BaseAction {

	private int flag = 0;
	private String msg;

	@Autowired
	private DataInfoService dataService;

	private List<DataInfo> datalist = new ArrayList<DataInfo>();

	public List<DataInfo> getDatalist() {
		return datalist;
	}

	public void setDatalist(List<DataInfo> datalist) {
		this.datalist = datalist;
	}

	/**
	 * 获取用户最新上传的数据
	 * 
	 * @throws UnsupportedEncodingException
	 */
	public void getLatestData() throws UnsupportedEncodingException {
		String userid = ServletActionContext.getRequest()
				.getParameter("userid").toString();
		userid = new String(userid.getBytes("ISO-8859-1"), "UTF-8");
		userid = java.net.URLDecoder.decode(userid, "UTF-8");

		JSONObject json = new JSONObject();
		List<DataInfo> SBP = dataService.getLatestDataById(
				Integer.valueOf(userid), (short) 1);
		List<DataInfo> DBP = dataService.getLatestDataById(
				Integer.valueOf(userid), (short) 2);
		List<DataInfo> BG = dataService.getLatestDataById(
				Integer.valueOf(userid), (short) 3);
		List<DataInfo> BOS = dataService.getLatestDataById(
				Integer.valueOf(userid), (short) 4);

		if (null != SBP && !SBP.isEmpty()) {
			json.put("SBP", SBP.get(0));// 收缩压
		}

		if (null != DBP && !DBP.isEmpty()) {
			json.put("DBP", DBP.get(0));// 舒张压
		}

		if (null != BG && !BG.isEmpty()) {
			json.put("BG", BG.get(0));// 血糖
		}

		if (null != BOS && !BOS.isEmpty()) {
			json.put("BOS", BOS.get(0));// 血氧饱和度
		}

		Json returnjson = new Json();
		if (!json.isEmpty()) {
			returnjson.setObj(json);
			returnjson.setSuccess(true);
			returnjson.setMsg("获取数据成功！");
		} else {
			returnjson.setSuccess(false);
			returnjson.setMsg("还没有测量哦！");
		}

		writeJson(returnjson);
	}

	// 查看历史数据列表
	public void historylist() throws UnsupportedEncodingException {
		Map<String, List> map = new HashMap<String, List>();
		Json json = new Json();

		String userid = ServletActionContext.getRequest()
				.getParameter("userid");
		String datatype = ServletActionContext.getRequest().getParameter(
				"datatype");
		String currpage = ServletActionContext.getRequest().getParameter(
				"currpage");
		String pagesize = ServletActionContext.getRequest().getParameter(
				"pagesize");

		int userid1 = Integer.valueOf(userid).intValue();
		short datatype1 = Short.valueOf(datatype);
		int currpage1 = Integer.valueOf(currpage).intValue();
		int pagesize1 = Integer.valueOf(pagesize).intValue();

		if (datatype1 == 1) {
			map = dataService.findBloodpressureData(userid1, datatype1,
					currpage1, pagesize1);

			if (!map.get("SBP").isEmpty()) {
				json.setSuccess(true);
				json.setObj(map);

			} else {
				json.setSuccess(false);
				json.setMsg("你还没有测量呢！");
			}

		} else {
			datalist = dataService.findDataWithOrders(userid1, datatype1,
					currpage1, pagesize1);

			if (!datalist.isEmpty()) {
				json.setSuccess(true);
				json.setObj(datalist);

			} else {
				json.setSuccess(false);
				json.setMsg("你还没有测量呢！");
			}

		}
		writeJson(json);

	}

	// 按天查看
	public void dataDay() throws UnsupportedEncodingException, ParseException {

		String userid = ServletActionContext.getRequest()
				.getParameter("userid");
		String datatype = ServletActionContext.getRequest().getParameter(
				"datatype");
		String date = ServletActionContext.getRequest().getParameter("date");

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");// 小写的mm表示的是分钟
		java.util.Date date1 = sdf.parse(date);
		// System.out.println(date1);

		Calendar rightNow = Calendar.getInstance();
		rightNow.setTime(date1);
		rightNow.add(Calendar.DAY_OF_YEAR, 1);
		// System.out.println(rightNow.getTime());

		Json json = new Json();
		datalist = this.dataService.getDateByDate(Integer.valueOf(userid),
				Short.valueOf(datatype), date1, rightNow.getTime());
		// System.out.println(datalist.get(0).getDataId());

		if (null != datalist && !datalist.isEmpty()) {
			json.setMsg("获取成功");
			json.setObj(datalist);
			json.setSuccess(true);
		} else {
			json.setMsg("这天没有数据哦！");
			json.setSuccess(false);
		}
		writeJson(json);
	}

}
